# (base)基础层：封装所有的通用操作(打开浏览器、打开网页、输入、点击...)
from selenium import webdriver
import time
import allure
from selenium.webdriver.common.by import By
from wordpress_project.base.logger import Logger
import os

# file = os.path.dirname(os.path.dirname(__file__))
# log_path = f'{file}/logs/wordpress.log'  # 日志存放路径


class Tools:
    def __init__(self,driver):
        """

        :param driver: 浏览器，只在你传递进来的浏览器操作
        :return:
        """
        # self.log = Logger(file_name=log_path)
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def visit(self,url):
        """

        :param url: 网页地址
        :return:
        """
        # self.log(f'正在访问{url}该网页')
        self.driver.get(url)
        time.sleep(0.5)

    def by_css(self,css):
        """根据css定位，返回定位的元素"""
        # self.log(f'正在使用css定位，表达式为{css}')
        return self.driver.find_element(By.CSS_SELECTOR,css)

    def by_link(self,link):
        """根据Link_Text 定位，返回定位的元素"""
        # self.log(f'正在使用文本链接定位，表达式为{link}')
        return self.driver.find_element(By.LINK_TEXT,link)



    def quit(self):
        self.driver.quit()

    def img(self):
        title = time.strftime('%Y_%m_%d_%H_%M_%S')
        # 截图、附加在测试报告中
        allure.attach(self.driver.get_screenshot_as_png(), title, allure.attachment_type.PNG)